<template>
  <div class="control-box">
    <div class="control-header">
      <span class="control-name">导航栏</span>
      <span @click="showBeautyBox = true">组件美化</span>
    </div>
    <div class="control-body">
      <div class="control-section">
        <!-- 模板样式 -->
        <div class="control-item">
          <div class="control-title">模板样式</div>
          <div class="control-content">
            <div class="example-list">
              <div
                class="example-item"
                v-for="(item, index) in exampleList"
                :class="swiperType == item.value ? 'active' : ''"
                @click="setStyleType(item)"
                :key="index"
              >
                <i class="iconfont" :class="item.icon" />
              </div>
            </div>
          </div>
        </div>
        <!-- 轮播点样式 -->
        <div class="control-item">
          <div class="control-title flex-between">
            <span>轮播点样式</span>
            <span class="reset" @click="resetItem('dots')">重置</span>
          </div>
          <div class="control-content">
            <a-select
              :default-value="params.params.dotsPosition"
              @change="
                (e) => {
                  setwithActionItem(e, 'dotsPosition');
                }
              "
              class="dotsPosition-select"
            >
              <a-select-option value="left"> 居左 </a-select-option>
              <a-select-option value="center"> 居中 </a-select-option>
              <a-select-option value="right"> 居右 </a-select-option>
            </a-select>
          </div>
        </div>
        <!-- 上传图片 -->
        <!-- 图片设置 -->
        <uploadImg
          type="list"
          :size="imgSize"
          :imgList="params.params.bannerList"
          :propActiveIndex="currentIndex"
          @uploadSuccess="uploadSuccess"
          @deleteImg="deleteImg"
          @changeImg="changeImg"
          @resetUpload="resetUpload"
          @handleImgItem="handleImgItem"
        ></uploadImg>
        <!-- 选择链接 -->
        <div
          class="choose-link"
          v-if="showChooseLink && params.params.bannerList.length > 0"
        >
          <chooseLink :link="currentItem" @chooseEnd="chooseLink"></chooseLink>
        </div>
        <!-- 图片高度 -->
        <div class="control-item">
          <div class="control-title flex-between">
            <span>图片高度</span>
            <span class="reset" @click="resetItem('swiperHeight')">重置</span>
          </div>
          <div class="control-content">
            <a-input
              class="diy-input"
              type="number"
              v-model="params.params.swiperHeight"
              @change="checkSetImgSize"
              suffix="px"
            />
          </div>
        </div>
      </div>
    </div>

    <!-- 组件美化 -->
    <comBeautyBox
      :showBeautyBox="showBeautyBox"
      @handleBack="showBeautyBox = false"
    >
      <div>
        <!-- 背景配置 -->
        <bgControl
          :backgroudData="params.moduleCss"
          sizeText="750*280"
        ></bgControl>
        <!-- 间距控制组件 -->
        <spaceControl
          :spaceData.sync="params.moduleCss"
          @resetSpace="resetSpace"
          @resetMp="resetMp"
        ></spaceControl>
      </div>
    </comBeautyBox>
  </div>
</template>

<script>
import comBeautyBox from "@/components/DiyNew/diy/comBeautyBox/comBeautyBox.vue";
import uploadImg from "@/components/DiyNew/uploadImg/index.vue";
import chooseLink from "@/components/DiyNew/chooseLink/index.vue";
import bgControl from "@/components/DiyNew/diy/baseControl/bgControl/index.vue";
import spaceControl from "@/components/DiyNew/diy/baseControl/spaceControl/index.vue";

export default {
  name: "swiperImgControl",
  components: {
    comBeautyBox,
    uploadImg,
    chooseLink,
    bgControl,
    spaceControl,
  },
  props: {
    params: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      showBeautyBox: false,
      exampleList: [
        {
          icon: "icon-zuoyouliubai",
          type: "模板1",
          value: "1",
        },
        {
          icon: "icon-quanpinglunbo",
          type: "模板2",
          value: "2",
        },
        {
          icon: "icon-xuanzhuanlunbo",
          type: "模板3",
          value: "3",
        },
      ],
      currentItem: {},
      currentIndex: -1,
      showChooseLink: false,
      imgSize: "750*316px",
    };
  },
  watch: {},
  computed: {
    swiperType: {
      get() {
        return this.params.params.swiperType || "1";
      },
    },
    imgList() {
      return this.params.params.bannerList;
    },
  },
  mounted() {
    if (this.imgList.length) {
      let item = this.imgList[0];
      this.currentItem = item.link;
      this.currentIndex = 0;
      this.showChooseLink = true;
      if (this.params.params.swiperHeight > 0) {
        this.imgSize = `750*${this.params.params.swiperHeight * 2}px`;
      }
    }
  },
  methods: {
    checkSetImgSize() {
      if (this.params.params.swiperHeight > 0) {
        this.imgSize = `750*${this.params.params.swiperHeight * 2}px`;
      }
    },
    // 设置样式
    setStyleType(item) {
      this.$setWithAction(this.params, "swiperType", item.value);
    },
    // 重置属性
    resetItem(type) {
      console.log(type);
      if (type == "dots") {
        this.$set(this.params.params, "dotsPosition", "center");
      }
      if (type == "banners") {
        this.$set(this.params.params, "bannerList", []);
      }
    },
    // 图片上传成功回调
    uploadSuccess(e) {
      console.log(e);
      let list = JSON.parse(JSON.stringify(this.params.params.bannerList));
      list.push({ picUrl: e.val, link: {} });
      this.$setWithAction(this.params, "bannerList", list);
    },
    // 更新图片
    changeImg(e) {
      console.log(e);
      let index = e.index;
      let list = JSON.parse(JSON.stringify(this.params.params.bannerList));
      for (let i = 0; i < list.length; i++) {
        if (i == index) {
          list[i].picUrl = e.val;
          break;
        }
      }
      this.$setWithAction(this.params, "bannerList", list);
    },
    // 删除图片
    deleteImg(e) {
      console.log(e);
      let index = e.index;
      let list = JSON.parse(JSON.stringify(this.params.params.bannerList));
      list.splice(index, 1);
      this.$setWithAction(this.params, "bannerList", list);
    },
    // 重置上传的图片
    resetUpload() {
      this.resetItem("banners");
    },
    // 点击单个图片
    handleImgItem(e) {
      let { item, index } = e;
      console.log(item, index);
      this.currentItem = item.link;
      this.currentIndex = index;
      this.showChooseLink = true;
    },
    // 选择链接
    chooseLink(e) {
      let index = this.currentIndex;
      let list = JSON.parse(JSON.stringify(this.params.params.bannerList));
      for (let i = 0; i < list.length; i++) {
        if (i == index) {
          list[i].link = e;
          break;
        }
      }
      this.$setWithAction(this.params, "bannerList", list);
      this.currentItem = list[index].link || {};
    },
    // 设置属性的同时添加action动作
    setwithActionItem(e, type) {
      console.log(e, type);
      this.$setWithAction(this.params, type, e);
    },
    // 重置圆角
    resetSpace(e) {
      let type = e.type;
      console.log(type);
      this.$set(this.params.moduleCss, type, 0);
    },
    // 重置边距
    resetMp() {
      this.$set(this.params.moduleCss, "margin", 0);
      this.$set(this.params.moduleCss, "padding", 0);
    },
  },
};
</script>

<style lang="scss" scoped>
.control-box {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  .control-header {
    width: 100%;
    height: 48px;
    background: #ffffff;
    border-bottom: 1px solid #f0f2f5;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px 14px 24px;
    span {
      font-size: 12px;
      color: #8c8c8c;
    }
    .control-name {
      font-size: 14px;
      font-weight: 600;
      color: #000000;
    }
    span:nth-child(2) {
      cursor: pointer;
    }
  }
  .control-body {
    width: 100%;
    .control-section {
      width: 228px;
      margin: 0 auto;
      text-align: center;
      .control-item {
        width: 100%;
        .control-title {
          padding: 24px 0 14px 0;
          font-size: 12px;
          color: #000000;
          text-align: left;
        }
        .flex-between {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
        .reset {
          font-size: 12px;
          color: #9e9e9e;
          cursor: pointer;
        }
        .control-content {
          .dotsPosition-select {
            width: 100%;
            height: 100%;
            ::v-deep .ant-select-selection--single {
              height: 100%;
              border: 0 none;
              background: #f2f2f2;
            }
            ::v-deep .ant-select-selection__rendered {
              line-height: 40px;
            }
          }
          .example-list {
            width: 100%;
            display: flex;
            flex-wrap: wrap;
            .example-item {
              width: 70px;
              height: 70px;
              background: #f6f7f9;
              border-radius: 4px;
              margin-right: 9px;
              display: flex;
              justify-content: center;
              align-items: center;
              cursor: pointer;
              overflow: hidden;
              .iconfont {
                font-size: 60px;
              }
            }
            .example-item:last-child {
              margin-right: 0;
            }
            .active {
              border: 2px solid #1890fc;
            }
          }
          .diy-input {
            width: 228px;
            height: 40px;
            background: #f6f7f9;
            border-radius: 4px;
            border: 0 none;
            ::v-deep.ant-input {
              width: 100%;
              height: 100%;
              border: 0 none;
            }
          }
        }
      }
      .choose-link {
        margin-top: 8px;
      }
    }
  }
}
</style>
